/**
 * Created by hetanhong on 2014/9/4.
 * 展示一事一表待办，一事一表查询详细内容，向后台提交内容
 */
(function () {
    var ysyb = function (config) {
        this._config = config;
        this._id = this.getConfig("id");//文件id
        this._taskName = this.getConfig("taskName");//流程名称
        this.width = window.innerWidth || 320;
        this.height = window.innerHeight || 480;
        this._buttonFlag = this.getConfig("buttonFlag");
        this._format = d3.time.format("%Y -%_m -%_d");//格式化时间
        this._url = this.getConfig("url") || "";//待办列表里的url，根据url判断是否为督办人
        // this._task = this.getTask();
        this._screenindex = this.getConfig("screenindex"); //上一步页面来源 1 待办 2 通知 3 应用 4 账户

    };
    if (typeof (Clickmed) === "undefined") {
        Clickmed = {};
    }
    Clickmed.ysyb = ysyb;
    ysyb.prototype.getTask = function () {
        var me = this;
        if (me._url.indexOf("task=1") > -1) {
            return 1;
        }
        if (me._url.indexOf("task=2") > -1) {
            return 2;
        }
        return 0;
    };
    ysyb.prototype.init = function () {
        var me = this;
        me._task = me.getTask();
        var url = serverURL + "cmcp/actionList/getToBeCreated";
        ;
        window.plugins.getIntent.show(["OA", "正在获取数据."]);

        var self = {
            id: me._id
        };
        PostCP(url, self, function (dat) {
            if (dat && (dat.length || objectJudge(dat))) {
                me.data = dat;
                me.create();
            }

        }, function () {
            window.plugins.getIntent.hide();
        });

        var self = me._id;

        console.log(me);
        me.draw();
        var url = serverURL + "cmcp/actionList/getActionList";

        PostCP(url, self, function (dat) {
            if (dat && (dat.length || objectJudge(dat))) {
                me.dat = dat;
                me.title();
            }
        }, function () {
            window.plugins.getIntent.hide();
        });
        var self = me._id;
        var url = serverURL + "cmcp/actionList/getAllActionListSVMesDTOs";
        PostCP(url, self, function (datt) {
            if (datt && (datt.length || objectJudge(datt))) {
                me.datt = datt;
                me.dbxx();
            }
        }, function () {
            window.plugins.getIntent.hide();
        });
        var url = serverURL + "cmcp/actionList/getAllActionListScheduleDTOs";
        PostCP(url, self, function (dattt) {
            if (dattt && (dattt.length || objectJudge(dattt))) {
                me.dattt = dattt;
                me.jdb();
            }
        }, function () {
            window.plugins.getIntent.hide();
        });
        var url = serverURL + "cmcp/actionList/getActionListItem";
        PostCP(url, self, function (datttt) {
            if (datttt && (datttt.length || objectJudge(datttt))) {
                me.datttt = datttt;
                me.xxjh()
            }
            me.datttt = datttt;
        }, function () {
            window.plugins.getIntent.hide();
        });
        //   var url =serverURL+"cmcp/actionList/setAllALMAndSDTOs"
    };
    //详细信息框架布局
    ysyb.prototype.draw = function () {
        var me = this;
        console.log(me);
        var me = this;
        var render = this._config.render || d3.select("body");
        var d1 = this._config.body || d3.select("body");
        var width = this.width;
        var scrol = d1.append("div").classed("scrol", true).style({
            width: "100%",
            height: "100%",
            position: "absolute",
            left: this.width + "px",
            top: "0px"
        }).attr("id", "scrol");
        d3.select(render).transition().duration(Transiacion).style("left", 0 - width + "px").style("position", "absolute").style({
            display: "none"
        });
        scrol.transition().duration(Transiacion).style("left", "0px");
        var warp = scrol.append("div").classed("dt_warp", true).style({
            position: "relative"
        });
        var datong_header = warp.append("div").classed("datong_header", true);
        var go_back = datong_header.append("div").classed("go_back", true);
        go_back.html("<i><img src =../../common/page/images/go_back_ico.png width =20 height=auto></i>返回");
        go_back.on(touch_s, function () {
            $(this).css("background-color", "#005ebf");
        });
        go_back.on(touch_e, function () {
            $(this).css("background-color", "");
            d1.selectAll(".scrol").transition().duration(Transiacion).style("left", width + "px");
            d3.select(render).transition().duration(Transiacion1).style("left", 0 + "px").style({
                display: "block"
            });
            d1.selectAll(".scrol").remove();
            setTimeout(function () {
                d1.selectAll(".scrol").remove();
                Android.back = TempFunction.back;
            }, Transiacion);
        });
        function back1() {
            d1.selectAll(".scrol").transition().duration(Transiacion).style("left", width + "px");
            d3.select(render).transition().duration(Transiacion1).style("left", 0 + "px").style({
                display: "block"
            });
            d1.selectAll(".scrol").remove();
            setTimeout(function () {
                d1.selectAll(".scrol").remove();
            }, Transiacion);
            Android.back = TempFunction.back;
        }

        // Android返回键动作
        Android.back = TempFunction.back1 = back1;
        datong_header.append("h1").html(me._taskName || "一事一表");
        me.Redraw();
    };
    ysyb.prototype.Redraw = function () {
        var me = this;
        var warp = d3.select("#scrol .dt_warp");
        var scrol_ = warp.append("div").classed("scrol_", true).style({
            width: "100%",
            height: height - 45 + "px",
            postion: "relative",
            overflow: "hidden"
        }).attr("id", "scrol_");
        var datong_con = scrol_.append("div").classed("datong_con datong_con_re", true);
        var div0 = datong_con.append("div").classed("div0", true);
        var div1 = datong_con.append("div").classed("div1", true);
        var div2 = datong_con.append("div").classed("div2", true);
        var div3 = datong_con.append("div").classed("div3", true);
        var div4 = datong_con.append("div").classed("div4", true);
        // console.log("hahah");
        window.plugins.getIntent.hide();
        newScroll = new iScroll("scrol_", {hScrollbar: false, vScrollbar: false});

    };
    //事项来源
    ysyb.prototype.create = function () {
        var me = this;
        me.docTitle = me.data["docTitle"];
        var scrol_ = d3.select("#scrol_");
        var datong_con = d3.select(".div0")
        var title = "事项来源";
        me._businessTitle = title;
        var datong_con = datong_con.append("div").classed("phone_cotent", true);
        datong_con.append("div").classed("title", true).style({
            "text-align": "center"
        }).html(title).classed("tongzhiTitle", true);

        var ul = datong_con.append("ul");
        var li = ul.append("li").classed("b_btm", true).style({
            "text-align": "center"
        });
        var lidl = li.append("dl").html('<dd>' + me.docTitle + '</dd>');

    };
    //标题，等详细信息
    ysyb.prototype.title = function () {
        var me = this;
        console.log(me.dat);

        me._dcRoleCode = me.dat["dcRoleCode"];
        var scrol_ = d3.select("#scrol_");
        var datong_con = d3.select(".div1")
        var title = me.dat["businessTitle"] || "一事一表";
        me._businessTitle = title;

        datong_con.append("div").classed("title", true).style({
            "text-align": "center"
        }).html(title).classed("tongzhiTitle", true);
        var datong_con = datong_con.append("div").classed("phone_cotent", true);
        var ul = datong_con.append("ul").style({
            "margin-top": "20px"
        });
        var li = ul.append("li").classed("b_btm", true).style({
            padding: "5px 0px"
        });
        var lidl = li.append("dl");
        lidl.append("dd").html("<b style='border-bottom:2px solid #2875c3'>事项内容&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><br>");
        lidl.append("dt").html(me.dat["actionContent"].replace(/\n/g, "<br>")).style({
            "text-indent": "2em"
        });

        if (me.dat["files"] && me.dat["files"].length) {
            var tempData = me.dat
            var fileList = datong_con.append("div").classed("report_title", true).html("附件");
            for (var i = 0; i < tempData["files"].length; i++) {
                var newDiv = datong_con.append("div").classed("newDiv", true)
                    .attr("url", tempData["files"][i]["pdfUrl"]).attr("fileName", tempData["files"][i]["name"]);
                var len = tempData["files"][i]["pdfUrl"].split(".").length;
                var extend = tempData["files"][i]["pdfUrl"].split(".")[len - 1];
                var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                    width: "25",
                    height: "22"
                }).style("margin-left", "15px");
                var file = newDiv.append("p").html(tempData["files"][i]["name"])
                    .attr("url", tempData["files"][i]["pdfUrl"]).attr("fileName", tempData["files"][i]["name"]);
                newDiv.on("click", function () {
                    var url = $(this).attr("url");
                    if (Flag)
                        return;
                    Flag = true;
                    setTimeout(function () {
                        Flag = false;
                    }, 500);
                    url = serverURL + url.substr(1);
                    var fileName = $(this).attr("fileName");
                    window.openF(url, fileName);
                });
            }
        }
        window.plugins.getIntent.hide();
        newScroll.refresh();
    };
    //督办信息
    ysyb.prototype.dbxx = function () {
        var me = this;
        var datt = me.datt;
        console.log(me);
        if (!datt)
            return;
        var scrol_ = d3.select("#scrol_");
        var datong_con = d3.select(".div3")
        var datong_con = datong_con.append("div").classed("phone_cotent", true);
        datong_con.append("div").classed("title", true).style({
            "text-align": "center"
        }).html("督办信息").classed("report_title", true);

        var ul = datong_con.append("ul");
        var li = ul.append("li").classed("b_btm", true).style({
            width: "100%"
        });
        var i = 0;
        if (datt && datt.length) {
            for (var i = 0; i < datt.length; i++) {
                var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0").style({
                    width: "100%"
                });
                if (i != 0) {
                    table.style({
                        "border-top":"3px solid #a2a2a2"
                    });
                }
                var tr = table.append("tr");
                tr.append("td").html("<div>督&nbsp;办&nbsp;时&nbsp;间</div>").style({
                    width: "25%"
                });
                tr.append("td").html(me._format(new Date(datt[i]["dcDate"]))).style({
                    width: "75%"
                });
                var tr = table.append("tr");
                tr.append("td").html("<div>督&nbsp;办&nbsp;内&nbsp;容</div>").style({
                    width: "25%"
                });
                tr.append("td").attr({
                    width: "75%"
                }).append("div").html(datt[i]["message"]).classed("extend", true);
                var tr = table.append("tr");
                tr.append("td").html("<div>回&nbsp;复&nbsp;内&nbsp;容</div>").style({
                    width: "25%"
                });
                if (datt[i]["reMessage"] && datt[i]["reMessage"].length) {
                    tr.append("td").attr({
                        width: "75%"
                    }).append("div").html(datt[i]["reMessage"]).classed("extend", true);
                }
                else if (me._task == 2) {
                    tr.append("td").style({
                        width: "75%"
                    }).append("div")
                        .append("button").style({
                            width: "50%",
                            "margin-left": "25%"
                        }).html("回&nbsp&nbsp&nbsp&nbsp复")
                        .classed("reMessage", true).attr("index", i).attr("oneId", datt[i]["id"]).attr("id", "reMessage" + i);


                    ;
                }
                else {
                    tr.append("td").style({
                        width: "75%"
                    }).append("div").html("").classed("extend", true);
                }


            }


        }
        if (me._task == 1) {
            var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0");
            var tr = table.append("tr");
            tr.append("td").html("<div>督&nbsp;办&nbsp;时&nbsp;间</div>").style({
                width: "25%"
            });
            tr.append("td").attr({
                width: "75%"
            }).append("div").html(me._format(new Date())).classed("extend", true);
            var tr = table.append("tr");
            tr.append("td").html("<div>督&nbsp;办&nbsp;内&nbsp;容</div>");
            tr.append("td").style({
                width: "75%"
            }).append("div").append("button").style({
                width: "50%",
                "margin-left": "25%"
            }).html("操&nbsp&nbsp&nbsp&nbsp作").classed("finish", true).attr("index", i).attr("id", "finish" + i);


        }
        $(".finish").on("click", function () {
            if (Flag)
                return;
            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            me._index = $(this).attr("index");
            //   me._oneId = $(this).attr("oneId");
            me.buttonClick("submit");
            console.log(me);
        });
        $(".reMessage").on("click", function () {
            if (Flag)
                return;
            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            me._oneId = $(this).attr("oneId");
            me._index = $(this).attr("index");
            me.buttonClick("reply");
        });
        window.plugins.getIntent.hide();
        newScroll.refresh();
    };
    //督办单
    ysyb.prototype.jdb = function () {
        var me = this;
        var dattt = me.dattt;
        if (!dattt || !dattt.length)
            return;
        var scrol_ = d3.select("#scrol_");
        var datong_con = d3.select(".div4");
        var datong_con = datong_con.append("div").classed("phone_cotent", true);
        datong_con.append("div").classed("title", true).style({
            "text-align": "center"
        }).html("进度表").classed("report_title", true);

        var ul = datong_con.append("ul");
        var li = ul.append("li").classed("b_btm", true).style({
            width: "100%"
        });
        for (var i = 0; i < dattt.length; i++) {
            var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0").style({
                width: "100%"
            });
            if (i != 0) {
                table.style({
                    "border-top":"3px solid #a2a2a2"
                });

            }
            var tr = table.append("tr");
            tr.append("td").html("<div>时&nbsp间</div>").style({
                width: "25%"
            });
            tr.append("td").attr({
                width: "75%"
            }).append("div").html(me._format(new Date(dattt[i]["scDate"]))).classed("extend", true);
            var tr = table.append("tr");
            tr.append("td").html("<div>内&nbsp容</div>").style({
                width: "25%"
            });
            tr.append("td").attr({
                width: "75%"
            }).append("div").html(dattt[i]["scMessage"]).classed("extend", true);
            var tr = table.append("tr");
            tr.append("td").html("<div>附&nbsp件</div>").style({
                width: "25%"
            });
            var td = tr.append("td").attr({
                width: "75%"
            }).append("div").classed("extend", true);
            var p = td.append("div");
            for (var j = 0; j < dattt[i]["files"].length; j++) {
                var file = p.append("p").html(dattt[i]["files"][j]["name"]).style({"margin": "2px 0px", color: "#28d"})
                    .attr("url", dattt[i]["files"][j]["pdfUrl"]).attr("fileName", dattt[i]["files"][j]["name"]);
                file.on("click", function () {
                    var url = $(this).attr("url");
                    if (Flag)
                        return;
                    Flag = true;
                    setTimeout(function () {
                        Flag = false;
                    }, 500);
                    url = serverURL + url.substr(1);
                    var fileName = $(this).attr("fileName");

                    window.openF(url, fileName);

                });
            }
        }
        window.plugins.getIntent.hide();
        newScroll.refresh();
    };
    //详细计划
    ysyb.prototype.xxjh = function () {
        var me = this;
        var dattt = me.datttt;
        if (!dattt || !dattt.length)
            return;
        var scrol_ = d3.select("#scrol_");
        var datong_con = d3.select(".div2");
        var datong_con = datong_con.append("div").classed("phone_cotent", true);
        datong_con.append("div").classed("title", true).style({
            "text-align": "center"
        }).html("详细计划").classed("report_title", true);

        var ul = datong_con.append("ul");
        var li = ul.append("li").classed("b_btm", true).style({
            width: "100%"
        });
        for (var i = 0; i < dattt.length; i++) {
            var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0").style({
                width: "100%"
            });
            table.style({
                "border-top":"3px solid  #a2a2a2"
            });
            var tr = table.append("tr");
            tr.append("td").html("<div>完&nbsp;成&nbsp;时&nbsp;间</div>").style({
                width: "25%"
            });
            tr.append("td").attr({
                width: "75%"
            }).append("div").html(me._format(new Date(dattt[i]["completeDate"]))).classed("extend", true);
            var tr = table.append("tr");
            tr.append("td").html("<div>责&nbsp;任&nbsp;人</div>").style({
                width: "25%"
            });
            tr.append("td").attr({
                width: "75%"
            }).append("div").html(dattt[i]["userName"]).classed("extend", true);
            var tr = table.append("tr");
            tr.append("td").html("<div>计&nbsp;划&nbsp;内&nbsp;容</div>").style({
                width: "25%"
            });
            tr.append("td").attr({
                width: "75%"
            }).append("div").html(dattt[i]["detailInfo"]).classed("extend", true);
            var tr = table.append("tr");
            tr.append("td").html("<div>状&nbsp&nbsp&nbsp&nbsp&nbsp态</div>").style({
                width: "25%"
            });
            tr.append("td").attr({
                width: "75%"
            }).append("div").html(dattt[i]["status"] == "0001" ? "未完成" : "完成").classed("extend", true);
            var tr = table.append("tr");
            tr.append("td").html("<div>备&nbsp;注&nbsp;信&nbsp;息</div>").style({
                width: "25%"
            });
            tr.append("td").attr({
                width: "75%"
            }).append("div").html(dattt[i]["completeMark"]).classed("extend", true);
        }
        window.plugins.getIntent.hide();
        newScroll.refresh();
    };
    //操作按钮事件
    ysyb.prototype.buttonClick = function (type) {
        var me = this;
        var d1 = this._config.body || d3.select("body");
        var select = d3.select("body").append("div").classed("select", true).style({
            width: "100%",
            height: "100%",
            background: "#eee",
            position: "absolute",
            left: width + "px",
            //   "z-index": 10,
            "over-follow": "hidden"
        });
        select.transition().duration(Transiacion).style("left", "0px");
        d1.select(".scrol").transition().duration(Transiacion).style("left", 0 - width + "px");
        var dt_warp = select.append("div").classed("dt_warp", true);
        var head = dt_warp.append("div").classed("datong_header", true);
        var back = head.append("div").classed("go_back", true);
        back.html("<i><img src =../../common/page/images/go_back_ico.png width =20 height=auto></i>返回");
        back.on(touch_s, function () {
            $(this).css("background-color", "#005ebf");
        });
        // 左上角返回按钮 动作
        back.on(touch_e, function () {
            $(this).css("background-color", "");
            d1.select(".scrol").transition().duration(Transiacion).style("left", 0 + "px");

            select.transition().duration(Transiacion).style("left", width + "px");
            setTimeout(function () {
                select.remove();
                // Android返回键动作更改
                Android.back = TempFunction.back1;
            }, 500);
        });
        head.append("h1").html(me._taskName);
        var datong_con = dt_warp.append("div").classed("datong_con", true).style({
            height: (height - 45) + "px",
            background: "#fff"
        });
        var ps_con = datong_con.append("div").classed("ps_con", true);
        var tj_select = ps_con.append("div").classed("tj_select", true).style({
        });
        var tj_con = tj_select.append("div").classed("div_input div_input2", true);
        if (type == "submit") {
            var label = tj_con.append("label").html("督办内容");
        }

        if (type == "reply") {
            var label = tj_con.append("label").html("回复内容");
        }
        var textarea = $("<textarea></textarea>").attr("rows", "12");
        $(tj_con.node()).append(textarea);
        var tj_button = datong_con.append("div").classed("tj_button2", true).classed("qb_btn_bg", true).style({
            bottom: "0px"
        });
        var cancel = tj_button.append("input").classed("btn_back", true).attr("type", "button")
            .attr("value", "取  消");
        var ensure = tj_button.append("input").classed("btn_agree", true).attr("type", "button")
            .attr("value", "确  认");

        ensure.on(touch_e, function () {
            if ($("textarea").val().length == 0) {
                Prompt("请填入信息", 1000);
                return;
            }
            if (Flag)
                return;
            Flag = true;

            setTimeout(function () {
                Flag = false;
            }, 500);
            // 退回到详细页面
            if (type == "submit") {
                var url = serverURL + "cmcp/report/doSuperviseActionList";
                var self = {
                    id: me._id,
                    message: $("textarea").val(),
                    dcDate: new Date()
                };
                var suc = function (dat) {
                    console.log(dat);
                    if (!dat) {
                        Prompt("提交失败", 1000);
                        return;
                    }
                    Prompt("提交成功", 1000);
                    $("#finish" + me._index).parent().parent().html($("textarea").val()).css({
                        "background": "none"
                    }).addClass("extend");;
                    select.transition().duration(Transiacion).style("left", width + "px");
                    d1.select(".scrol").transition().duration(Transiacion).style("left", 0 + "px");
                    setTimeout(function () {
                        select.remove();
                        // Android 返回键动作更改
                        Android.back = TempFunction.back1;
                    }, Transiacion);
                }
            }
            if (type == "reply") {
                var url = serverURL + "cmcp/actionList/setAllALMAndSDTOs";
                var self = {
                    businessId: me._id,
                    dcRoleCode: me._dcRoleCode,
                    businessTitle: me._businessTitle,
                    actionListSuperviseMessageDTOs: [
                        {
                            id: me._oneId,
                            reMessage: $("textarea").val()
                        }
                    ]
                };
                var suc = function (dat) {
                    console.log(dat);
                    if (dat != "success") {
                        Prompt("提交失败", 1000);
                        return;
                    }
                    Prompt("提交成功", 1000);

                    $("#reMessage" + me._index).parent().parent().html($("textarea").val()).css({
                        "background": "none"
                    }).addClass("extend");;
                    select.transition().duration(Transiacion).style("left", width + "px");
                    d1.select(".scrol").transition().duration(Transiacion).style("left", 0 + "px");
                    setTimeout(function () {
                        select.remove();
                        // Android 返回键动作更改
                        Android.back = TempFunction.back1;
                    }, Transiacion);
                };
            }
            console.log(url, self);
            PostCP(url, self, suc, function () {
                Prompt("提交失败", 1000);
            });

        });
        cancel.on(touch_e, function () {
            if (Flag)
                return;
            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            // 退回到详细页面
            select.transition().duration(Transiacion).style("left", width + "px");
            d1.select(".scrol").transition().duration(Transiacion).style("left", 0 + "px");
            setTimeout(function () {
                select.remove();
                // Android 返回键动作更改
                Android.back = TempFunction.back1;
            }, Transiacion);
        });
        // Android 返回键动作
        function back2() {
            $(".comList").remove();
            $(".mark").remove();
            select.transition().duration(Transiacion).style("left", width + "px");
            d1.select(".scrol").transition().duration(Transiacion).style("left", 0 + "px");
            setTimeout(function () {
                select.remove();
            }, 500);
            Android.back = TempFunction.back1;
        }

        Android.back = TempFunction.back2 = back2;
    };
    ysyb.prototype.getConfig = function (key) {
        if (this._config == null) {
            return null;
        } else {
            if (typeof (this._config[key]) != "undefined") {
                return this._config[key];
            } else {
                return null;
            }
        }
    };
})();